# -*- coding: utf-8 -*-
import copy
import math
import random
import time, MySQLdb    
import datetime
from pyevolve import GSimpleGA
from pyevolve import G1DList
from pyevolve import Selectors
from pyevolve import Initializators, Mutators
import pyevolve
lot_num=[1,1,14,14,12,12,12,1,1,1,1,7,7,7,8,8,8,5,5,5,5]
f=open('打包-修复-1000-1-1-150-200.txt','w+')
list_dic={'IF':[],'CU':[],'I':[],'M':[],'RU':[],'SR':[],'RB':[],'TA':[],'Y':[]}
list_margin={'IF':100000,'CU':30000,'RU':23000,'I':3500,'M':2100,'RB':2500,'SR':4500,'TA':3600,'Y':5400}
index1=[2, 3,4, 5, 6,14, 15, 16,11, 12, 13,17, 18, 19, 20]
index2=[7, 8, 9, 10,0, 1]
list_zhonglei=[]
conn=MySQLdb.connect(host="localhost",user="root",passwd="root",db="chaodata",charset="utf8")  
cursor = conn.cursor()
sql="select pinPrefix,leverRatio from pro_information ;"
nn=cursor.execute(sql)
row_dic=cursor.fetchall()
dic_ratio=dict(row_dic)

sql="select pinPrefix,contractUnit from pro_information ;"
nn=cursor.execute(sql)
row_dic=cursor.fetchall()
dic_unit=dict(row_dic)
 
x_len=21
for i in xrange(x_len):
    sql="select * from data where ID=%s ;"%i
    m=cursor.execute(sql)
    row=cursor.fetchone()
    list_zhonglei.append(row[0])
    list_dic[row[0].upper()].append(i)
print 'ok'

def mystd(a):
    l=len(a)
    m=sum(a)/float(l)
    d=0
    for i in a: d+=(i-m)**2
    return math.sqrt(d/l)
def output_test(list_lots):
    global start
    global mid
    s=start.strftime('%y-%m-%d')
    m=mid.strftime('%y-%m-%d')
    mm=m
    all_row_test=[]
    #print s,m,list_lots[0:]
    for i in xrange(len(list_lots)):
        if list_lots[i]>0:
            sql="select * from data where ID=%s and Date>='%s' and Date<'%s';"%(i,s,mm)
            #print s,mm
            m=cursor.execute(sql)
            row=cursor.fetchall()
            all_row_test=all_row_test+list(row)
    return all_row_test
def output_run(list_lots):
    global mid
    global end
    m=mid.strftime('%y-%m-%d')
    e=end.strftime('%y-%m-%d')
    mm=m
    ee=e
    all_row_run=[]
    for i in xrange(len(list_lots)):
        if list_lots[i]>0:
            sql="select * from data where ID=%s and Date>='%s' and Date<'%s' ;"%(i,mm,ee)
            m=cursor.execute(sql)
            row=cursor.fetchall()
            all_row_run=all_row_run+list(row)
    return all_row_run
c=0
def test(list_signal,ll):
    global num_test
    global num_lost
    num_test=num_test+1
    a=ll[0:]
    l=copy.deepcopy(a)
    for ii in xrange(21):
        l[ii]=l[ii]*lot_num[ii]
    global c
    global dic_ratio
    global dic_unit
    total_money=10000000
    total_cost=0
    #buy_lot=0
    #sell_lot=0
    #buy_price=0
    #sell_price=0
    buy_lot=[0]*len(l)
    sell_lot=[0]*len(l)
    buy_price=[0]*len(l)
    sell_price=[0]*len(l)

    profit_rate=[]
    profit=[]
    totalprofit=[]
    for i in list_signal:
        if c==1:
            print sell_lot
            print sell_price
        symbol=i[0].upper()
        ID=int(i[-1])
        if buy_lot[ID]==0 and i[2]==-2:
            continue
        if sell_lot[ID]==0 and i[2]==2:
            continue
        lot=l[ID]
        if i[2]==1:
            buy_lot[ID]=buy_lot[ID]+lot
            total_money=total_money-i[5]*lot
            buy_price[ID]=(buy_price[ID]*(buy_lot[ID]-lot)+i[3]*lot)/buy_lot[ID]
            
            
        if i[2]==-1:
            sell_lot[ID]=sell_lot[ID]+lot
            total_money=total_money-i[5]*lot
            sell_price[ID]=(sell_price[ID]*(sell_lot[ID]-lot)+i[3]*lot)/sell_lot[ID]
            
        if i[2]==-2:
            buy_lot[ID]=buy_lot[ID]-lot
            total_money=total_money+i[5]*lot
            pro=((i[3]-buy_price[ID])*dic_unit[symbol]-i[-2]*2)*lot
            profit_rate.append(pro/(buy_price[ID]*dic_unit[symbol]*lot))
            profit.append(pro)
            total_money=total_money+profit[-1]
            #print profit[-1]
            totalprofit.append(sum(profit))
            if buy_lot[ID]==0:
                buy_price[ID]=0
        if i[2]==2:
            sell_lot[ID]=sell_lot[ID]-lot
            total_money=total_money+i[5]*lot
            pro=((sell_price[ID]-i[3])*dic_unit[symbol]-i[-2]*2)*lot
            profit_rate.append(pro/(sell_price[ID]*dic_unit[symbol]*lot))
            profit.append(pro)
            total_money=total_money+profit[-1]
            totalprofit.append(sum(profit))
            if sell_lot[ID]==0:
                sell_price[ID]=0
        if total_money<0:
            num_lost=num_lost+1
            #print 'wrong',total_money
            return 0
##    if sell_lot!=0 or buy_lot!=0:
##        print "wrong"
    #print totalprofit[-1]
    if len(profit)==0:
        return 0
    s=sum(profit_rate)/(mystd(profit_rate)*math.sqrt(len(profit_rate)))
    if s<0:
        s=0
    return s
def run(list_signal,ll):
    
    print ll,1
    global dic_ratio
    global dic_unit
    l=copy.deepcopy(ll)
    for ii in xrange(21):
        l[ii]=l[ii]*lot_num[ii]
    print l,2
    total_money=10000000
    total_cost=0
    #buy_lot=0
    #sell_lot=0
    #buy_price=0
    #sell_price=0
    buy_lot=[0]*len(l)
    sell_lot=[0]*len(l)
    buy_price=[0]*len(l)
    sell_price=[0]*len(l)
    profit_rate=[]
    profit=[]
    totalprofit=[]
    for i in list_signal:
        
        symbol=i[0].upper()
        ID=int(i[-1])
        if buy_lot[ID]==0 and i[2]==-2:
            continue
        if sell_lot[ID]==0 and i[2]==2:
            continue
        lot=l[ID]
        if i[2]==1:
            buy_lot[ID]=buy_lot[ID]+lot
            total_money=total_money-i[5]*lot
            buy_price[ID]=(buy_price[ID]*(buy_lot[ID]-lot)+i[3]*lot)/buy_lot[ID]
            
            
        if i[2]==-1:
            sell_lot[ID]=sell_lot[ID]+lot
            total_money=total_money-i[5]*lot
            sell_price[ID]=(sell_price[ID]*(sell_lot[ID]-lot)+i[3]*lot)/sell_lot[ID]
            
        if i[2]==-2:
            buy_lot[ID]=buy_lot[ID]-lot
            total_money=total_money+i[5]*lot
            pro=((i[3]-buy_price[ID])*dic_unit[symbol]-i[-2]*2)*lot
            profit_rate.append(pro/(buy_price[ID]*dic_unit[symbol]*lot))
            profit.append(pro)
            total_money=total_money+profit[-1]
            #print profit[-1]
            totalprofit.append(sum(profit))
            if buy_lot[ID]==0:
                buy_price[ID]=0
        if i[2]==2:
            sell_lot[ID]=sell_lot[ID]-lot
            total_money=total_money+i[5]*lot
            pro=((sell_price[ID]-i[3])*dic_unit[symbol]-i[-2]*2)*lot
            profit_rate.append(pro/(sell_price[ID]*dic_unit[symbol]*lot))
            profit.append(pro)
            total_money=total_money+profit[-1]
            totalprofit.append(sum(profit))
            if sell_lot[ID]==0:
                sell_price[ID]=0
        if total_money<0:
            return []
    print "########right"
    return profit

def main_test(l):
    #修复算法：
    s=0
    for i in xrange(x_len):
        s=s+l[i]*list_margin[list_zhonglei[i]]*lot_num[i]
    if s>10000000:
        index=range(0,x_len)
        while(1):
            ii=random.choice(index)
            index.remove(ii)
            if l[ii]>0:
                l[ii]=l[ii]-1
                s=s-list_margin[list_zhonglei[ii]]*lot_num[ii]
            if s<=10000000:
                break
            if len(index)==0:
                index=range(0,x_len)
    all_row=output_test(l[0:])
    if len(all_row)==0:
        return 0
    all_row.sort(key=lambda x:x[1])
    a=test(all_row,l[:])
    return a
def main_run(l):
    all_row=output_run(l)
    if len(all_row)==0:
        return []
    all_row.sort(key=lambda x:x[1])
    a=run(all_row,l)
    return a

test_days=30
run_days=30
t1=datetime.timedelta(days=test_days)
t2=datetime.timedelta(days=run_days)
start=datetime.datetime(2011,3,23,0,0,0)
stop=datetime.datetime(2013,12,7,0,0,0)
mid=start+t1
end=start+t2+t1
row_test=[]
row_run=[]

##genome = G1DList.G1DList(x_len)
###genome.setParams(rangemin=0,rangemax=2)
##genome.My_set(list_dic,list_margin,1000000,list_zhonglei)
##genome.initializator.set(Initializators.G1DListInitializatorInteger_my)#G1DListInitializatorInteger_my)
##genome.mutator.set(Mutators.G1DListMutatorInteger_my)
##genome.evaluator.set(main_test)
##genome.crossover.clear()
##ga = GSimpleGA.GSimpleGA(genome)
##ga.selector.set(Selectors.GRouletteWheel)
##ga.setMutationRate(0.8)
##ga.setPopulationSize(20)
##ga.setGenerations(20)

temp=[]
f_per=open('打包-修复-1000-1-1-150-200-per.txt','w+')
while(end<=stop):
    num_test=0
    num_lost=0
    s=start.strftime('%y-%m-%d')
    m=mid.strftime('%y-%m-%d')
    e=end.strftime('%y-%m-%d')
    f.write(s+' '+m+' '+e+'\n')
    print end
    genome = G1DList.G1DList(x_len)
    genome.setParams(range_list=[20,20,10,10,20,20,20,30,30,30,30,20,20,20,20,20,20,20,20,20,20])
    genome.initializator.set(Initializators.G1DListInitializatorInteger)
    genome.mutator.set(Mutators.G1DListMutatorIntegerGaussian)
    genome.evaluator.set(main_test)
    genome.crossover.clear()
    ga = GSimpleGA.GSimpleGA(genome)
    ga.selector.set(Selectors.GRouletteWheel)
    ga.setMutationRate(0.9)
    ga.setPopulationSize(200)
    ga.setGenerations(400)
    ga.evolve(20)
    best=ga.bestIndividual()
    #row_test=output_test([1])
    #row_run=output_run([0,1])
##    if len(row_test)>0 and len(row_run)>0:
##        print len(row_test),1
##        print len(row_run),2
    per=num_lost/float(num_test)
    f_per.write(str(per))
    f_per.write('\n')
    print num_lost,num_test
    print best[0:]
    a=best[0:]
    for ii in xrange(21):
        a[ii]=a[ii]*lot_num[ii]
    a=str(a)
    f.write(a+'\n')
    temp=temp+main_run(best[0:])
    start=start+t2
    mid=mid+t2
    end=end+t2
print len(temp)
proo=[]
for i in xrange(len(temp)):
    proo.append(sum(temp[:i+1]))
x=xrange(len(proo))
from pylab import*
plot(x,proo)
savefig(r'打包-修复-1000-1-1-150-200.png')
f_per.close()
f.close()
conn.close()
cursor.close()
